package cn.ekuma.android.ui.filter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import cn.ekuma.data.ui.android.R;
import android.widget.ImageButton;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2014/12/4.
 */
public class BaseFilterPanel extends LinearLayout {
    ImageButton btnHandle;
    Button btnAdvSeach,btnReset;
    View AdvSeachView;
    boolean isAdvSeachModel,isInitAdvSeachModel;
    LinearLayout headleLayout;


    public BaseFilterPanel(Context context, FilterPanelAdapter adapter) {
        super(context);
        init(context, adapter);
    }

    private void init(final Context context, final FilterPanelAdapter adapter) {
        View mainView = LayoutInflater.from(context).inflate( R.layout.base_filter, this);
        isInitAdvSeachModel=false;

        btnReset=(Button) mainView.findViewById(R.id.btnReset);
        btnReset.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                adapter.filterParaReset();
            }
        });
        isAdvSeachModel = false;

        btnHandle = (ImageButton) mainView.findViewById(R.id.btnHandle);
        headleLayout = (LinearLayout) mainView.findViewById(R.id.headPanel);
        btnHandle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (headleLayout.getVisibility() != View.GONE) {
                    headleLayout.setVisibility(View.GONE);
                    ((ImageButton) v)
                            .setImageResource(R.drawable.cand_down_press);
                } else {
                    headleLayout.setVisibility(View.VISIBLE);
                    ((ImageButton) v)
                            .setImageResource(R.drawable.cand_up_press);
                }
            }
        });
        View c = adapter.createFilterPanel();
        if (c != null)
            headleLayout.addView(c);
        else{
            btnHandle.setVisibility(View.GONE);
            btnReset.setVisibility(View.GONE);
        }

        btnAdvSeach = (Button) mainView.findViewById(R.id.btnAdvSeach);
        AdvSeachView =adapter.createAdvFilterPanel();
        if (AdvSeachView == null)
            btnAdvSeach.setVisibility(View.GONE);
        else{
            headleLayout.addView(AdvSeachView);
            AdvSeachView.setVisibility(View.GONE);
        }

        btnAdvSeach.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if(!isInitAdvSeachModel){
                    adapter.initAdvSeachView();
                    isInitAdvSeachModel=true;
                }
                if (AdvSeachView.getVisibility() != View.GONE) {
                    AdvSeachView.setVisibility(View.GONE);
                    isAdvSeachModel = false;
                } else {
                    AdvSeachView.setVisibility(View.VISIBLE);
                    isAdvSeachModel = true;
                }
            }
        });
    }

   public void expandView(){
       this.setVisibility(View.VISIBLE);
   }

   public void unExpandView(){
       this.setVisibility(View.GONE);
   }


    public interface FilterPanelAdapter{
        public abstract  void filterParaReset();

        public  abstract View  createFilterPanel();

        public abstract View createAdvFilterPanel();

        public abstract void initAdvSeachView();
    }
}
